package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import pailaidai.hc.com.mysvgdemo.R;

/**
 * Created by Administrator on 2018/4/3.
 */

public class CustomAddView extends LinearLayout {
    private LayoutInflater mInflater;
    private TableLayout layout;
    private int tableRowID;
    private int dynamicBtnID;
    private int dynamicPreID;
    private int dynamicAftID;
    private List<View> idList = new ArrayList<>();


    public CustomAddView(Context context) {
        super(context);
    }

    public CustomAddView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
        layout = (TableLayout) mInflater.inflate(R.layout.zy_dynamicvalues, null);
        //设置默认值
        View view = layout.findViewById(R.id.dynameicvalues_button);
        view.setOnClickListener(getClickListener());
        idList.add(view);
        tableRowID = R.id.dynameic_parent;
        dynamicBtnID = R.id.dynameicvalues_button;
        dynamicAftID = R.id.dynameic_afc;
        dynamicPreID = R.id.dynameic_pre;
        addView(layout);
    }

    private OnClickListener getClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View view) {
                clickLogic(view);
            }
        };
    }

    private void clickLogic(View view) {
        if (!idList.isEmpty()) {
            if (view == idList.get(idList.size() - 1)) {
                addCountrols(view);
            } else {
                removeControls(view);
            }
        }

    }

    private void removeControls(View view) {
        if (idList.size() <= 1) {
            return;
        }
        TableLayout parent = (TableLayout) view.getParent();
        ((TableLayout) parent.getParent()).removeView(parent);
    }

    private void addCountrols(View view) {
        layout.addView(getNewRow());
        ((ImageButton) view).setImageResource(R.mipmap.ic_launcher);
    }

    private TableRow getNewRow() {
        TableRow row = getDefaultTableRow();
        row.addView(getDefaultImageButton());
        row.addView(getDefaultPrevEdotText());
        row.addView(getDefaultTextView());
        row.addView(getDefaultAfterEditText());
        return row;
    }


    public CustomAddView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TableRow getDefaultTableRow() {
        tableRowID++;
        TableRow row = new TableRow(getContext());
        TableRow.LayoutParams params = new TableRow.LayoutParams();
        params.width = TableRow.LayoutParams.WRAP_CONTENT;
        params.height = TableRow.LayoutParams.WRAP_CONTENT;
        row.setLayoutParams(params);
        row.setGravity(Gravity.CENTER);
        row.setId(tableRowID);
        return row;
    }

    public View getDefaultImageButton() {
          dynamicBtnID++;
        ImageButton button=new ImageButton(getContext());
        button.setAdjustViewBounds(true);
        TableRow.LayoutParams params=new TableRow.LayoutParams();
        params.width=TableRow.LayoutParams.WRAP_CONTENT;
        params.height=TableRow.LayoutParams.WRAP_CONTENT;
        button.setLayoutParams(params);
        button.setImageResource(R.mipmap.ic_launcher);
        button.setId(dynamicBtnID);
        button.setOnClickListener(getClickListener());
        idList.add(button);
        return button;
    }

    public View getDefaultPrevEdotText() {
        dynamicPreID++;
        EditText text = getDefaultEditText();
        text.setId(dynamicPreID);
        return text;
    }

    public View getDefaultTextView() {
        TextView textView=new TextView(getContext());
        TableRow.LayoutParams params=new TableRow.LayoutParams();
        params.width=TableRow.LayoutParams.WRAP_CONTENT;
        params.height=TableRow.LayoutParams.WRAP_CONTENT;
        textView.setLayoutParams(params);
        textView.setText("—");
        return textView;
    }

    public View getDefaultAfterEditText() {
        dynamicAftID++;
        EditText text = getDefaultEditText();
         text.setId(dynamicAftID);
        return text;
    }

    public EditText getDefaultEditText() {
       EditText text=new EditText(getContext());
        TableRow.LayoutParams params=new TableRow.LayoutParams();
        params.width=TableRow.LayoutParams.WRAP_CONTENT;
        params.height=TableRow.LayoutParams.WRAP_CONTENT;
        text.setMaxLines(1);
        text.setWidth(80);
        return text;
    }
}
